package com.lb.testweb.controller;


import com.lb.testservice.service.CommoditySerivce;
import com.lb.tetcommon.bean.Commodity;
import com.lb.tetcommon.bean.CommonResult;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/commodity")
public class CommodityContoller {
    @Resource
    private CommoditySerivce commoditySerivce;

    @PostMapping(value = "/insert")
    public CommonResult insert(Commodity commodity){
        int n = commoditySerivce.insertSelective(commodity);
        System.out.println("*****插入操作返回结果："+n);
        if(n>0){
            return new CommonResult(200,"插入数据库成功",n);
        }else {
            return new CommonResult(406,"插入数据库失败",n);
        }
    }

    @PostMapping(value = "/delete")
    public CommonResult delete(Integer cid){
        int n = commoditySerivce.deleteByPrimaryKey(cid);
        System.out.println("*****删除操作返回结果："+n);
        if(n>0){
            return new CommonResult(200,"数据删除成功",n);
        }else {
            return new CommonResult(406,"数据删除失败",n);
        }
    }

    @PostMapping(value = "/update")
    public CommonResult update(Commodity commodity){
        int n = commoditySerivce.updateByPrimaryKeySelective(commodity);
        System.out.println("*****修改操作返回结果："+n);
        if(n>0){
            return new CommonResult(200,"修改数据成功",n);
        }else {
            return new CommonResult(406,"修改数据失败",n);
        }
    }

    @RequestMapping(value = "/select")
    public CommonResult selectAll(Model model){
        List<Commodity> commodities = commoditySerivce.selectAll();
        System.out.println("*****查询操作返回查询条数："+commodities.size());
        if(commodities.size()>0){
            model.addAttribute("commoditieAll",commodities);
            return new CommonResult(200,"查询数据成功",commodities);
        }else {
            return new CommonResult(404,"查询数据失败",null);
        }

    }



}
